package com.edu.controller;

import com.edu.domain.Resource;
import com.edu.domain.ResourseVo;
import com.edu.domain.ResponseResult;
import com.edu.service.ResourceService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/resource")
@CrossOrigin("*")
public class ResourceController {

    @Autowired
    public ResourceService resourceService;

    @RequestMapping("/findAllResource")
    public ResponseResult findAllResourceByPage(@RequestBody ResourseVo resourseVo){

        PageInfo<Resource> pageInfo = resourceService.findAllResourceByPage(resourseVo);

        return  new ResponseResult(true,200,"资源信息分页多条件查询成功",pageInfo);

    }

    /*
        添加&更新资源信息
     */
    @RequestMapping("/saveOrUpdateResource")
    public ResponseResult saveOrUpdateResource(@RequestBody Resource resource){

        if(resource.getId() == null){
            resourceService.saveResource(resource);
            return new ResponseResult(true,200,"资源信息添加成功","");
        }else {
            resourceService.updateResource(resource);
            return new ResponseResult(true,200,"资源信息更新成功","");
        }
    }

    /*
        删除资源信息
    */

    @RequestMapping("/deleteResource")
    public ResponseResult deleteResource(Integer id){

        resourceService.deleteResource(id);

        return new ResponseResult(true,200,"资源信息删除成功","");
    }

}
